/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.google.common.base.Strings;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.BEncoding;
/*     */ import com.zimbra.common.util.BEncoding.BEncodingException;
/*     */ import com.zimbra.common.util.BlobMetaData;
/*     */ import com.zimbra.common.util.BlobMetaDataEncodingException;
/*     */ import java.io.PrintStream;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.SortedMap;
/*     */ import java.util.TreeMap;
/*     */ import org.apache.commons.codec.Charsets;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class Metadata
/*     */ {
/*     */   public static final int LEGACY_METADATA_VERSION = 10;
/*     */   public static final String FN_ATTRS = "a";
/*     */   @Deprecated
/*     */   public static final String FN_RIGHTS = "acl";
/*     */   public static final String FN_RIGHTS_MAP = "aclm";
/*     */   public static final String FN_ALARM_DATA = "ad";
/*     */   public static final String FN_ACCOUNT_ID = "aid";
/*     */   public static final String FN_CALITEM_IDS = "ais";
/*     */   public static final String FN_CALITEM_END = "ape";
/*     */   public static final String FN_CALITEM_START = "aps";
/*     */   public static final String FN_ATTACHMENTS = "att";
/*     */   public static final String FN_COLOR = "c";
/*     */   public static final String FN_CAL_INTENDED_FOR = "cif";
/*     */   public static final String FN_COMPONENT = "comp";
/*     */   public static final String FN_CREATOR = "cr";
/*     */   public static final String FN_MIME_TYPE = "ct";
/*     */   public static final String FN_DRAFT = "d";
/*     */   public static final String FN_DISABLE_ACTIVESYNC = "das";
/*     */   public static final String FN_WEB_OFFLINE_SYNC_DAYS = "wosd";
/*     */   public static final String FN_DESCRIPTION = "de";
/*     */   public static final String FN_DESC_ENABLED = "dee";
/*     */   public static final String FN_REPLY_ORIG = "do";
/*     */   public static final String FN_REPLY_TYPE = "dt";
/*     */   public static final String FN_AUTO_SEND_TIME = "ast";
/*     */   public static final String FN_ENTRIES = "en";
/*     */   public static final String FN_FRAGMENT = "f";
/*     */   public static final String FN_FIRST = "f";
/*     */   public static final String FN_FIELDS = "fld";
/*     */   public static final String FN_DELETED = "i4d";
/*     */   public static final String FN_DELETED_UNREAD = "i4du";
/*     */   public static final String FN_RECENT = "i4l";
/*     */   public static final String FN_RECENT_CUTOFF = "i4r";
/*     */   public static final String FN_REMOTE_ID = "id";
/*     */   public static final String FN_IDENTITY_ID = "idnt";
/*     */   public static final String FN_INV = "inv";
/*     */   public static final String FN_BOUNDS = "l";
/*     */   public static final String FN_LAST_DATE = "ld";
/*     */   public static final String FN_LOCK_OWNER = "lo";
/*     */   public static final String FN_LISTED = "lst";
/*     */   public static final String FN_LOCK_TIMESTAMP = "lt";
/*     */   public static final String FN_MODSEQ = "mseq";
/*     */   public static final String FN_NUM_COMPONENTS = "nc";
/*     */   public static final String FN_NODES = "no";
/*     */   public static final String FN_PREFIX = "p";
/*     */   public static final String FN_PARTICIPANTS = "prt";
/*     */   public static final String FN_QUERY = "q";
/*     */   public static final String FN_RAW_SUBJ = "r";
/*     */   public static final String FN_REV_DATE = "rd";
/*     */   public static final String FN_REVISIONS = "rev";
/*     */   public static final String FN_REV_ID = "rid";
/*     */   public static final String FN_REPLY_LIST = "rl";
/*     */   public static final String FN_RETENTION_POLICY = "rp";
/*     */   public static final String FN_REV_SIZE = "rs";
/*     */   public static final String FN_REPLY_TO = "rt";
/*     */   public static final String FN_REMOTE_UUID = "ruuid";
/*     */   public static final String FN_SENDER = "s";
/*     */   public static final String FN_SORT = "s";
/*     */   public static final String FN_SYNC_DATE = "sd";
/*     */   public static final String FN_SYNC_GUID = "sg";
/*     */   public static final String FN_REMINDER_ENABLED = "rem";
/*     */   public static final String FN_TOTAL_SIZE = "sz";
/*     */   public static final String FN_RECIPIENTS = "t";
/*     */   public static final String FN_TYPES = "t";
/*     */   public static final String FN_TZMAP = "tzm";
/*     */   public static final String FN_UID = "u";
/*     */   public static final String FN_USER_AGENT = "ua";
/*     */   public static final String FN_UIDNEXT = "unxt";
/*     */   public static final String FN_URL = "url";
/*     */   @Deprecated
/*     */   public static final String FN_MD_VERSION = "v";
/*     */   public static final String FN_METADATA_VERSION = "mdver";
/*     */   public static final String FN_VERSION = "ver";
/*     */   public static final String FN_VIEW = "vt";
/*     */   public static final String FN_WIKI_WORD = "ww";
/*     */   public static final String FN_ELIDED = "X";
/*     */   public static final String FN_EXTRA_DATA = "xd";
/*     */   private final Integer associatedItemId;
/*     */   Map<Object, Object> map;
/*     */   
/*     */   public Metadata()
/*     */   {
/* 131 */     this.associatedItemId = null;
/* 132 */     this.map = new TreeMap();
/*     */   }
/*     */   
/*     */   public Metadata(Map<?, ?> map) {
/* 136 */     this.associatedItemId = null;
/* 137 */     this.map = new TreeMap(map);
/*     */   }
/*     */   
/*     */   public Metadata(String encoded) throws MailServiceException {
/* 141 */     this(encoded, (Integer)null);
/*     */   }
/*     */   
/*     */   public Metadata(String encoded, Integer associatedItemId) throws MailServiceException {
/* 145 */     this.associatedItemId = associatedItemId;
/* 146 */     if (Strings.isNullOrEmpty(encoded)) {
/* 147 */       this.map = new HashMap();
/* 148 */       return;
/*     */     }
/*     */     try
/*     */     {
/* 152 */       this.map = ((Map)BEncoding.decode(encoded));
/*     */ 
/*     */ 
/*     */     }
/*     */     catch (BEncoding.BEncodingException be)
/*     */     {
/*     */ 
/* 159 */       if ((be.getCause() != null) && ((be.getCause() instanceof NumberFormatException))) {
/* 160 */         String fixedUpEncoded = new String(encoded.getBytes(Charsets.ISO_8859_1), Charsets.UTF_8);
/*     */         try {
/* 162 */           this.map = ((Map)BEncoding.decode(fixedUpEncoded));
/*     */           
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 177 */           if ((this.map != null) && (this.map.containsKey("v"))) {
/*     */             try {
/* 179 */               this.map.remove("v");
/*     */             }
/*     */             catch (Exception e) {}
/*     */           }
/*     */           return;
/*     */         }
/*     */         catch (Exception e) {}
/*     */       }
/* 167 */       throw be;
/*     */     }
/*     */     catch (BEncoding.BEncodingException e) {
/*     */       try {
/* 171 */         this.map = BlobMetaData.decodeRecursive(encoded, associatedItemId);
/*     */         
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 177 */         if ((this.map != null) && (this.map.containsKey("v"))) {
/*     */           try {
/* 179 */             this.map.remove("v");
/*     */           }
/*     */           catch (Exception e) {}
/*     */         }
/*     */       }
/*     */       catch (BlobMetaDataEncodingException e1)
/*     */       {
/* 175 */         throw MailServiceException.INVALID_METADATA(encoded, e);
/*     */       }
/* 177 */     } finally { if ((this.map != null) && (this.map.containsKey("v"))) {
/*     */         try {
/* 179 */           this.map.remove("v");
/*     */         }
/*     */         catch (Exception e) {}
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean containsKey(String key) {
/* 187 */     return this.map.containsKey(key);
/*     */   }
/*     */   
/*     */   public int size() {
/* 191 */     return this.map.size();
/*     */   }
/*     */   
/*     */   public boolean isEmpty() {
/* 195 */     return this.map.isEmpty();
/*     */   }
/*     */   
/*     */   public Metadata copy(Metadata source) {
/* 199 */     if (source != null) {
/* 200 */       this.map.putAll(source.map);
/*     */     }
/* 202 */     return this;
/*     */   }
/*     */   
/*     */   public Map<String, ?> asMap() {
/* 206 */     Map<String, Object> result = new HashMap();
/* 207 */     for (Map.Entry<Object, Object> entry : this.map.entrySet()) {
/* 208 */       Object key = entry.getKey();
/* 209 */       Object value = entry.getValue();
/* 210 */       if ((key != null) && (value != null))
/*     */       {
/* 212 */         if ((value instanceof Map)) {
/* 213 */           result.put(key.toString(), new Metadata((Map)value));
/* 214 */         } else if ((value instanceof List)) {
/* 215 */           result.put(key.toString(), new MetadataList((List)value));
/*     */         } else
/* 217 */           result.put(key.toString(), value);
/*     */       }
/*     */     }
/* 220 */     return result;
/*     */   }
/*     */   
/*     */   public Metadata remove(String key) {
/* 224 */     this.map.remove(key);
/* 225 */     return this;
/*     */   }
/*     */   
/*     */   public Metadata put(String key, Object value) {
/* 229 */     if ((key != null) && (value != null)) {
/* 230 */       this.map.put(key, value);
/*     */     }
/* 232 */     return this;
/*     */   }
/*     */   
/*     */   public Metadata put(String key, long value) {
/* 236 */     if (key != null) {
/* 237 */       this.map.put(key, Long.valueOf(value));
/*     */     }
/* 239 */     return this;
/*     */   }
/*     */   
/*     */   public Metadata put(String key, double value) {
/* 243 */     if (key != null) {
/* 244 */       this.map.put(key, new Double(value));
/*     */     }
/* 246 */     return this;
/*     */   }
/*     */   
/*     */   public Metadata put(String key, boolean value) {
/* 250 */     if (key != null) {
/* 251 */       this.map.put(key, new Boolean(value));
/*     */     }
/* 253 */     return this;
/*     */   }
/*     */   
/*     */   public Metadata put(String key, Metadata value) {
/* 257 */     if ((key != null) && (value != null)) {
/* 258 */       this.map.put(key, value.map);
/*     */     }
/* 260 */     return this;
/*     */   }
/*     */   
/*     */   public Metadata put(String key, MetadataList value) {
/* 264 */     if ((key != null) && (value != null)) {
/* 265 */       this.map.put(key, value.list);
/*     */     }
/* 267 */     return this;
/*     */   }
/*     */   
/*     */   public String get(String key) throws ServiceException {
/* 271 */     return Element.checkNull(key, get(key, null));
/*     */   }
/*     */   
/*     */   public long getLong(String key) throws ServiceException {
/* 275 */     return Element.parseLong(key, Element.checkNull(key, get(key, null)));
/*     */   }
/*     */   
/*     */   public double getDouble(String key) throws ServiceException {
/* 279 */     return Element.parseDouble(key, Element.checkNull(key, get(key, null)));
/*     */   }
/*     */   
/*     */   public boolean getBool(String key) throws ServiceException {
/* 283 */     return Element.parseBool(key, Element.checkNull(key, get(key, null)));
/*     */   }
/*     */   
/*     */   public String get(String key, String defaultValue) {
/* 287 */     Object value = this.map.get(key);
/* 288 */     return value == null ? defaultValue : value.toString();
/*     */   }
/*     */   
/*     */   public long getLong(String key, long defaultValue) throws ServiceException {
/* 292 */     String raw = get(key, null);
/* 293 */     return raw == null ? defaultValue : Element.parseLong(key, raw);
/*     */   }
/*     */   
/*     */   public int getInt(String key, int defaultValue) throws ServiceException {
/* 297 */     String raw = get(key, null);
/* 298 */     return raw == null ? defaultValue : Element.parseInt(key, raw);
/*     */   }
/*     */   
/*     */   public short getShort(String key, short defaultValue) throws ServiceException {
/* 302 */     String raw = get(key, null);
/* 303 */     return raw == null ? defaultValue : Element.parseShort(key, raw);
/*     */   }
/*     */   
/*     */   public double getDouble(String key, double defaultValue) throws ServiceException {
/* 307 */     String raw = get(key, null);
/* 308 */     return raw == null ? defaultValue : Element.parseDouble(key, raw);
/*     */   }
/*     */   
/*     */   public boolean getBool(String key, boolean defaultValue) throws ServiceException {
/* 312 */     String raw = get(key, null);
/* 313 */     return raw == null ? defaultValue : Element.parseBool(key, raw);
/*     */   }
/*     */   
/*     */   public MetadataList getList(String key) throws ServiceException {
/* 317 */     return getList(key, false);
/*     */   }
/*     */   
/*     */   public MetadataList getList(String key, boolean nullOK) throws ServiceException {
/* 321 */     Object value = this.map.get(key);
/* 322 */     if ((nullOK) && (value == null)) {
/* 323 */       return null;
/*     */     }
/* 325 */     if ((value instanceof List))
/*     */     {
/* 327 */       List<Object> cast = (List)value;
/* 328 */       return new MetadataList(cast);
/*     */     }
/* 330 */     throw ServiceException.INVALID_REQUEST("invalid/missing value for attribute: " + key, null);
/*     */   }
/*     */   
/*     */   public Metadata getMap(String key) throws ServiceException {
/* 334 */     return getMap(key, false);
/*     */   }
/*     */   
/*     */   public Metadata getMap(String key, boolean nullable) throws ServiceException {
/* 338 */     Object value = this.map.get(key);
/* 339 */     if ((nullable) && (value == null)) {
/* 340 */       return null;
/*     */     }
/* 342 */     if ((value instanceof Map))
/*     */     {
/* 344 */       Map<String, Object> cast = (Map)value;
/* 345 */       return new Metadata(cast);
/*     */     }
/* 347 */     throw ServiceException.INVALID_REQUEST("invalid/missing value for attribute: " + key, null);
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 352 */     put("v", 10L);
/* 353 */     String result = BEncoding.encode(this.map);
/* 354 */     this.map.remove("v");
/* 355 */     return result;
/*     */   }
/*     */   
/*     */   public String prettyPrint() {
/* 359 */     StringBuilder sb = new StringBuilder(2048);
/* 360 */     prettyEncode(sb, this.map, 0);
/* 361 */     sb.setLength(sb.length() - 1);
/* 362 */     return sb.toString();
/*     */   }
/*     */   
/*     */   private static StringBuilder prettyEncode(StringBuilder sb, Object object, int indentLevel) {
/* 366 */     if ((object instanceof Map)) {
/* 367 */       SortedMap<?, ?> tree = (object instanceof SortedMap) ? (SortedMap)object : new TreeMap((Map)object);
/*     */       
/* 369 */       sb.append("{\n");
/* 370 */       for (Map.Entry<?, ?> entry : tree.entrySet()) {
/* 371 */         if ((entry.getKey() != null) && (entry.getValue() != null)) {
/* 372 */           appendIndent(sb, indentLevel + 1);
/* 373 */           sb.append(entry.getKey()).append(" = ");
/* 374 */           prettyEncode(sb, entry.getValue(), indentLevel + 1);
/*     */         }
/*     */       }
/* 377 */       appendIndent(sb, indentLevel);
/* 378 */       sb.append("}\n");
/* 379 */     } else if ((object instanceof List)) {
/* 380 */       sb.append("[\n");
/* 381 */       for (Object value : (List)object) {
/* 382 */         if (value != null) {
/* 383 */           appendIndent(sb, indentLevel + 1);
/* 384 */           prettyEncode(sb, value, indentLevel + 1);
/*     */         }
/*     */       }
/* 387 */       appendIndent(sb, indentLevel);
/* 388 */       sb.append("]\n");
/* 389 */     } else if (((object instanceof Long)) || ((object instanceof Integer)) || ((object instanceof Short)) || ((object instanceof Byte))) {
/* 390 */       sb.append(object).append("\n");
/* 391 */     } else if (object != null) {
/* 392 */       sb.append(object.toString()).append("\n");
/*     */     }
/* 394 */     return sb;
/*     */   }
/*     */   
/*     */   private static void appendIndent(StringBuilder sb, int indentLevel) {
/* 398 */     int num = indentLevel * 2;
/* 399 */     for (int i = 0; i < num; i++) {
/* 400 */       sb.append(' ');
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws MailServiceException {
/* 405 */     String encoded = "d1:ai1e4:aclmd1:gld1:ei0e1:g36:474b7021-cef6-469d-b5fb-54c96117efd11:ri1e1:ti1eee2:gei0e2:iei0ee5:mdveri4e4:mseqi628e2:szi7596456e4:unxti744e1:vi10e2:vti5ee";
/* 406 */     Metadata meta = new Metadata(encoded);
/* 407 */     Map<String, ?> map = meta.asMap();
/* 408 */     for (String key : map.keySet()) {
/* 409 */       System.out.println("key: " + key + " value: " + map.get(key));
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/Metadata.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */